/*
 *  Check Class
 *
 *      Natural Ordering - tableNumber
 *
 *
 *
 */

package project7.Storage;

import java.util.ArrayList;

public class Check implements Comparable {

    /*------ Private Variables -----------------------------------------------*/
    private int serverCode = 0;
    private int checkNumber = 0;
    private ArrayList<MenuItem> itemsOrdered = null;
    private double total = 0.0;
    private int tableNumber = 0;

    /*------ Constructors ----------------------------------------------------*/
    
    public Check(int tableNum) {
        tableNumber = tableNum;
        itemsOrdered = new ArrayList<MenuItem>();
    }

    public Check(int checkNum, int servCode, ArrayList items, double tot, int tNum) {
        checkNumber = checkNum;
        serverCode = servCode;
        itemsOrdered = items;
        total = tot;
        tableNumber = tNum;

    }

    /*------ Operations ------------------------------------------------------*/
    
    public boolean addItem(MenuItem item) {
        total += item.getPrice();
        return itemsOrdered.add(item);
    }

    public boolean addItem(MenuItem item, int count) {
        boolean success = false;
        total += item.getPrice() * count;
            for(int i = 0; i < count; i++)
                success = itemsOrdered.add(item);
        return success;
    }

    public boolean removeItem(MenuItem item) {
        total -= item.getPrice();
        return itemsOrdered.remove(item);
    }

    public double getRoundedTotal(){
        double roundedTotal = Math.round(total * 100);
        return roundedTotal / 100;
    }

    /*------ Accessors and Mutators ------------------------------------------*/
    
    public int getServerCode(){ return serverCode; }
    public void setServerCode(int code){ serverCode = code; }

    public ArrayList<MenuItem> getItemsOrdered(){ return itemsOrdered; }
    public void setItemsOrdered(ArrayList<MenuItem> a){ itemsOrdered = a; }

    public double getTotal(){ return total; }
    public void setTotal(double d){ total = d; }

    public int getTableNumber(){ return tableNumber; }
    public void setTableNumber(int i){ tableNumber = i; }

    /*------ Overrides -------------------------------------------------------*/
    
    @Override
    public String toString() {
        String str = "";
        double roundedTotal = Math.round(total * 100);
        for (int i = 0; i < itemsOrdered.size(); i++) {
            str += itemsOrdered.get(i) + "\n";
        }
        str += "Total = " + roundedTotal / 100;

        return str;
    }

    public int compareTo(Object o) {
        Check temp = (Check) o;
        return this.getTableNumber() - temp.getTableNumber();
    }
}
